#!/bin/bash
#
# request key through web interface
#

#    (c) 2011 Donders Centre/Simon Oosthoek
#
#    This file is part of PIM.
#
#    PIM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    PIM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PIM.  If not, see <http://www.gnu.org/licenses/>.
#
function print_usage {
	echo "
Process Information on Mentat (PIM)

PIM is used to monitor and clean up old processes that users have
forgotten about on the Mentat Cluster. This script can help you 
interact with PIM from the commandline, if you are not  
inside the DCCN.

USAGE:
pim \"<action key>\"
or
pim <hostname> <pid>

The <action key> is the garbled text you received in an e-mail and 
it can either be a renew command or a kill command, 
so pay attention to which one you copy-paste ;-)

In the e-mail you have received the complete commandline, so e.g.

pim \"MDAzIDUzNpkgbmljYnl1IHJuZXcgMzxyMTI4MmFjwWU5CXR=\"

You can also specify a hostname and a process id (pid), e.g.:
pim mentat234 1234

" >&2
	exit 1
}

case $# in
	1)	links -dump -dump-width 80 "http://pim.fcdonders.nl/pidaction.php?key=$1" |
		awk '	/PIM logo/ { infoseen=1 }
			/^References/ { references=1 }
			infoseen == 1 && references != 1 { print }' |
		sed 's/\[.*\].*PIM logo//; s/^ *//' |
		boxes -f /opt/cluster/boxes-config -d simple
	;;
	
	2) links -dump -dump-width 80 "http://pim.fcdonders.nl/pidinfo.php?host=$1&pid=$2" | 
		awk '	/info about this process/ { infoseen=1 ; getline}
			/^References/ { references=1 }
			infoseen == 1 && references != 1 { print }'
	;;
	*)
		print_usage
	;;

esac

exit 0
